//
//  LZCouponPresenter.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCouponPresenter.h"
#import "NetworkServiceManager.h"
#import "NSString+Additions.h"
#import "MJExtension.h"

#import "LZCouponListCouponDetailModel.h"
#import "LZCouponListModel.h"
#import "LZUserModel.h"

extern NSString *merchant;
static NSString *const kCouponDetialURL = @"/promotion/info"; //优惠券详情
static NSString *const kCouponStateCountURL = @"/promotion/promotionNumber"; //优惠类型数量
static NSString *const kCouponListURL = @"/promotion/search"; //优惠列表
static NSString *const kCouponModifyURL = @"/promotion/modify"; //修改 或者 编辑 优惠
static NSString *const kPublishCouponURL = @"/promotion/add"; //创建优惠

@implementation LZCouponPresenter

#pragma mark -
#pragma mark 获取优惠详情 包含 优惠券详情 团购详情
/**
 获取优惠券详情
 @param couponId 优惠id
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getCouponDetailWithCouponID:(NSString *)couponId ResultSuccess:(void (^)(LZCouponListCouponDetailModel *))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kCouponDetialURL);
    NSDictionary *parmeter = @{@"id" : couponId};
    
   [[NetworkServiceManager manager] executePostTask:urlStr parameters:parmeter success:^(NSURLSessionDataTask *task, id resultData) {
       
       if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
           LZCouponListCouponDetailModel *m = [LZCouponListCouponDetailModel mj_objectWithKeyValues:resultData[@"data"]];
           if (successBlock) {
               successBlock(m);
           }
       }
   } fail:^(NSURLSessionDataTask *task, NSError *error) {
       SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
   }];
}

#pragma mark -
#pragma mark 优惠类型数量

/**
 获取 优惠类型数量
 
 @param sellerId 商户ID
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getCouponStateCountWithSellerID:(NSString *)sellerId ResultSuccess:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant, kCouponStateCountURL);
    NSDictionary *dic = @{@"sellerId" : sellerId};
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            if (successBlock) {
                successBlock(resultData[@"data"]);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
}

#pragma mark -
#pragma mark 获取优惠列表
/**
 获取优惠列表
 
 @param sellerId 商户id
 @param cState 列表状态 1 已提交 2 待审核 3 审核通过 4 审核退回 5 已发布 6 已下线
 @param couponName 优惠名称  ==搜索用
 @param cPage 当前页数
 @param limit <#limit description#>
 @param successBlcok <#successBlcok description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getCouponListWithSellerID:(NSString *)sellerId CouponState:(NSString *)cState CouponName:(NSString *)couponName Cpage:(NSInteger)cPage Limit:(NSInteger)limit ResultSuccess:(void (^)(NSArray *, NSInteger, NSInteger, NSTimeInterval))successBlcok Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kCouponListURL);
    NSMutableDictionary *dic = @{}.mutableCopy;
    if (cState) {
        dic[@"state"] = cState;
    }
    
    if (couponName) {
        dic[@"state"] = couponName;
    }
    
    dic[@"sellerId"] = sellerId;
    dic[@"cpage"] = [NSString stringWithFormat:@"%ld",(long)cPage];
    dic[@"limit"] = [NSString stringWithFormat:@"%ld",(long)limit];
    
    [[NetworkServiceManager manager]executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            NSInteger totalPageTemp = [resultData[@"data"][@"totalPages"] integerValue];
            NSInteger currentPageTemp = [resultData[@"data"][@"cPageNO"] integerValue];
            NSTimeInterval timeStampTemp = [resultData[@"timestamp"] doubleValue];
            NSMutableArray *modelArr = [[NSMutableArray alloc] init];
            NSArray *arr = resultData[@"data"][@"list"];
            for (NSInteger i = 0; i< arr.count; i ++) {
                LZCouponListModel *m = [LZCouponListModel mj_objectWithKeyValues:arr[i]];
                [modelArr addObject:m];
            }
            
            if (successBlcok) {
                successBlcok(modelArr,totalPageTemp,currentPageTemp, timeStampTemp);
            }
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
         SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
    
}


#pragma mark -
#pragma mark 优惠列表中修改优惠状态
/**
 在优惠列表中点击 提交 发布 下线 时进行的操作
 
 @param couponId 优惠id
 @param couponState 状态 1 已保存 2 待审核 3 审核通过 4 审核退回 5 已发布 6 已下线
 @param isDelete 状态 1 未删除 0 删除
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)inCouponListModifyCouponWithCouponId:(NSString *)couponId couponState:(int)couponState couponIsDelete:(int)isDelete ResultSuccess:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    if (![[LZUserModel Instance] isLogin]) {
        if (failureBlock) {
            failureBlock(@"用户未登录");
        }
        
        return;
    }
    
    NSString *urlStr = COMBINATION(merchant,kCouponModifyURL);
    NSDictionary *dic = @{@"id" : couponId,
                          @"state" : [NSNumber numberWithInt:couponState],
                          @"isDelete" : [NSNumber numberWithInt:isDelete],
                          @"token" : [LZUserModel Instance].userToken
                          };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            if (successBlock) {
                successBlock(resultData[@"data"]);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
}

#pragma mark -
#pragma mark 编辑优惠,重新保存
/**
 编辑优惠 重新提交
 
 @param couponModel 原来的优惠模型 需要再次模型中把 state 状态改为 1(已经保存)
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)modifyCouponWithCouponModel:(LZCouponListModel *)couponModel ResultSuccess:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", merchant, kCouponModifyURL];
    NSLog(@"%@", urlStr);
//    NSString *urlStr = combination(merchant,kCouponModifyURL);
    NSMutableDictionary *dic = couponModel.mj_keyValues;
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    [dic setObject:[LZUserModel Instance].sellerId forKey:@"sellerId"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            
            NSLog(@"修改优惠：%@", resultData);
            
            if (successBlock) {
                successBlock(nil);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
}

#pragma mark -
#pragma mark 发布优惠

/**
 发布最新的优惠
 
 @param localCouponModel 本地组建的model.不包含 优惠id 等关键信息
 @param successBlock 成功返回信息最全的model.包好 优惠id
 @param failureBlock <#filureBlock description#>
 */
- (void)publishCouponWithLocalCouponModel:(LZCouponListModel *)localCouponModel ResultSuccess:(void(^)(LZCouponListModel *completeCouponModel))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", merchant, kPublishCouponURL];
    NSLog(@"%@", urlStr);
    
    NSMutableDictionary *dic = localCouponModel.mj_keyValues;
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    [dic setObject:[LZUserModel Instance].sellerId forKey:@"sellerId"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:failureBlock]) {
            
            NSLog(@"发布优惠：%@", resultData);
            
            LZCouponListModel *modelTemp = [LZCouponListModel mj_objectWithKeyValues:resultData[@"data"]];
            if (successBlock) {
                successBlock(modelTemp);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(failureBlock, error.localizedDescription);
    }];
}


@end
